﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx;
using Styx.Helpers;
using Styx.Logic;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Logic.Pathing;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Combat;
using System.Timers;
using System.Diagnostics;

namespace SwInYUtility
{
    class RareKiller
    {
        private static Stopwatch _SWTimer = new Stopwatch();

        internal static void Initialize()
        {
            Log.Write(Log.eLogType.RareKiller, "Loaded!");
            Log.WriteDebug( Log.eLogType.RareKiller,"Enabled: {0}", Settings._Instance.RareKiller_Enabled);
        }

        internal static void Pulse()
        {
            if (Battlegrounds.IsInsideBattleground) return;
            if (StyxWoW.Me.Combat) return;

            // Make List of Rare's
            WoWUnit Rare = ObjectManager.GetObjectsOfType<WoWUnit>().Where( e => e.IsAlive && !e.IsFriendly && e.Attackable && !Blacklist.Contains(e.Guid) && e.Level >= Settings._Instance.RareKiller_MinLevel &&
                    (e.CreatureRank == WoWUnitClassificationType.Rare || (Settings._Instance.RareKiller_KillElites && e.CreatureRank == WoWUnitClassificationType.RareElite))).FirstOrDefault();

            if (Rare == null) return;

            Log.Write(Log.eLogType.RareKiller, "-------------------------------");
            Log.Write(Log.eLogType.RareKiller, "--------- Found Rare! ---------");
            Log.Write(Log.eLogType.RareKiller, "-------------------------------");
            Log.Write(Log.eLogType.RareKiller, "Name: {0}", Rare.Name);
            Log.Write(Log.eLogType.RareKiller, "Level: {0}", Rare.Level);
            Log.Write(Log.eLogType.RareKiller, "Type: {0}", Rare.CreatureRank.ToString());
            Log.Write(Log.eLogType.RareKiller, "Health: {0}/{1}", Rare.CurrentHealth, Rare.MaxHealth);
            Log.Write(Log.eLogType.RareKiller, "XYZ: {0}", Rare.Location.ToString());
            Log.Write(Log.eLogType.RareKiller, "-------------------------------");

            _SWTimer.Start();

            while (!StyxWoW.Me.Combat && StyxWoW.IsInGame && StyxWoW.IsInWorld && Rare.IsValid && StyxWoW.Me.IsAlive && _SWTimer.ElapsedMilliseconds < 25000)
            {
                MoveTo(Rare);
            }

            if (_SWTimer.ElapsedMilliseconds > 25000) Blacklist.Add(Rare.Guid, new TimeSpan(0, 15, 0));
            _SWTimer.Reset();
        }




        internal static void MoveTo(WoWUnit Rare)
        {
            // Fly to him
            if (StyxWoW.Me.IsFlying)
            {
                Log.WriteDebug(Log.eLogType.RareKiller, "Flying To: {0} - [{1}]", Rare.Name, Rare.Location.ToString());
                Flightor.MoveTo(Rare.Location);
            }

            // Walk
            if (!StyxWoW.Me.IsFlying)
            {
                if (Navigator.CanNavigateFully(StyxWoW.Me.Location, Rare.Location))
                {
                    Log.WriteDebug(Log.eLogType.RareKiller, "Moving To: {0} - [{1}]", Rare.Name, Rare.Location.ToString());
                    Navigator.MoveTo(Rare.Location);
                }
                else 
                {
                    Blacklist.Add(Rare.Guid, new TimeSpan(0,15,0));
                }
            }

            if (Rare.Distance < 2)
            {
                LootTargeting.LootMobs = true;

                if (!Rare.IsFlying) Mount.Dismount();

                Log.WriteDebug(Log.eLogType.RareKiller, "Pulling {0}", Rare.Name);
                Rare.Target();
                RoutineManager.Current.Pull();
                RoutineManager.Current.Combat();
                _SWTimer.Reset();
            }
        }

    }
}
